<div class="mws-panel grid_8">
    <div class="mws-panel-header">
        <span>Escolha o tipo da saída</span>
    </div>
    <div class="mws-panel-body min-panel">
        <div class="mws-panel-content">

            <?php
            $arrayTipoSaida = array();
            foreach ($data[0] as $tipoSaida) {
                echo "<div class='list-saida gradienC btn'>";
                echo "<div class='img-saida'>";
                echo "<img src='" . BASE_URL . "uploads/" . $tipoSaida->imagem . "' >";
                echo "</div>";

                echo "<label>" . $tipoSaida->descricao . "<label>";
                echo "</div>";

                $arrayTipoSaida[] = $tipoSaida->codigo;
            }
            ?>


        </div>
    </div>
</div>

<script>
    arrayTip = <?php echo json_encode($arrayTipoSaida); ?>;
    $('.list-saida').on('click', function() {
        var cod = arrayTip[$(this).index()];
        var img = $('.img-saida', $(this)).html();
        var txt = $('.label', $(this)).html();
        openModaForm(img, cod, txt);
    });

    function openModaForm(img, cod, txt) {
        $('#modal').load("<?php echo BASE_URL; ?>saida/form");
        $('#modal').dialog({
            modal: true,
            title: 'Inserir Saída',
            width: 600,
            resizable: false,
            buttons: {
                Salvar: function() {
                    submitForm(cod);
                },
                Cancelar: function() {
                    $('#modal').dialog("close");
                }
            }
        });
    }

    function submitForm(cod) {
        if (__validaForm()) {
            mensg = "";
            $.ajax({
                type: "post",
                data: "tipo=" + cod + "&valor=" + $("#valor").val()+ "&descricao=" + $("#descricao").val(),
                url: '<?php echo BASE_URL; ?>saida/insert',
                success: function(j) {
                    if (j != "erro") {
                        mensg = "sucesso";
                    } else {
                        mensg = "erro";
                    }
                    $('#modal').dialog("close");
                    addNotify(mensg);
                }
            });
        }
    }
</script>